import os

import uiautomator2 as u2
import subprocess
from tools.function import get_device


def open_dingstock():
    """连接到设备，冷启动盯潮app，返回driver"""
    device = get_device()
    con = u2.connect_usb(device)
    con.app_start(package_name='cool.dingstock.mobile', activity='.activity.index.HomeIndexActivity', stop=True)
    con.xpath('//*[@resource-id="cool.dingstock.mobile:id/circle_activity_topic_detail_publish_iv"]').wait(20)
    con.implicitly_wait(30)
    return con


def open_dingstock_hot():
    """连接到设备，热启动盯潮app，返回driver"""
    device = get_device()
    con = u2.connect_usb(device)
    con.app_start(package_name='cool.dingstock.mobile', activity='.activity.index.HomeIndexActivity', stop=False)
    con.xpath('//*[@resource-id="cool.dingstock.mobile:id/circle_activity_topic_detail_publish_iv"]').wait(20)

    return con


def open_device_atx_server():
    """启动连接设备的atx-agent服务"""
    # subprocess.Popen('adb shell;/data/local/tmp/atx-agent server -d', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    # subprocess.Popen('/data/local/tmp/atx-agent server -d', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    os.system('adb shell')
    os.system('/data/local/tmp/atx-agent server -d')


